package cn.sczhyt.modules.szhy.controller;

import java.util.List;

import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.sys.entity.SysUserInfoEntity;
import cn.sczhyt.modules.sys.service.SysUserInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.sczhyt.modules.szhy.entity.UserGroupEntity;
import cn.sczhyt.modules.szhy.service.UserGroupService;



/**
 * 群组基本信息表
 *
 * @author luoshijie
 * @email 1169414426@qq.com
 * @date 2020-09-08 15:12:05
 */
@Api(tags = "PC端群组管理")
@RestController
@RequestMapping("szhy/usergroup")
public class UserGroupController {
    @Autowired
    private UserGroupService userGroupService;
    @Autowired
    private SysUserInfoService sysUserInfoService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    //@RequiresPermissions("szhy:usergroup:list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "20") Integer limit,
                  @RequestBody(required = false) UserGroupEntity userGroupEntity){
        IPage<UserGroupEntity> iPage=userGroupService.findList(page,limit,userGroupEntity);
        PageUtils list = new PageUtils(iPage);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{groupId}")
    @ApiOperation("信息")
    //@RequiresPermissions("szhy:usergroup:info")
    public R info(@PathVariable("groupId") String groupId){
		UserGroupEntity userGroup = userGroupService.getById(groupId);

        List<SysUserInfoEntity> sysUserInfoEntities=sysUserInfoService.findList();
        userGroup.setSysUserInfoEntityList(sysUserInfoEntities);

        return R.ok().put("userGroup", userGroup);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    //@RequiresPermissions("szhy:usergroup:save")
    public R save(@RequestBody UserGroupEntity userGroup){

        if(userGroup.getGroupId()==null){
            userGroupService.save(userGroup);
        }else{
            userGroupService.updateById(userGroup);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    //@RequiresPermissions("szhy:usergroup:update")
    public R update(@RequestBody UserGroupEntity userGroup){
		userGroupService.updateById(userGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
   // @RequiresPermissions("szhy:usergroup:delete")
    public R delete(@RequestParam String groupId){

        userGroupService.removeById(groupId);

        return R.ok();
    }

}
